#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

int testShuffle(struct gameState *state, int playerId) {
	printf("\n\tPlayer %d shuffle results:\n", playerId);
	int oldDeckCount = 50;
	int oldDeck[oldDeckCount];
	int sameCards = 0;
	int sameOrder = 0;
	int i, j, k;
	int cardsCounted[50];
		for(i=0;i<50;i++) { cardsCounted[i] = -1;}
	int thisCardCounted = 0;
	//make copy of current deck
	oldDeckCount = state->deckCount[playerId];
	for(i=0;i<oldDeckCount;i++) {
		oldDeck[i] = state->deck[playerId][i];
	}
	//call the shuffle function
	shuffle(playerId, state);
	//assert deckCount is the same
	assert(oldDeckCount == state->deckCount[playerId]);
	//test the results for all cards present
	for (i=0;i<oldDeckCount;i++) {
		k = 0;
		for (j=0;j<oldDeckCount;j++) {
			if ((oldDeck[i] == state->deck[playerId][j])) {
				//check that the 'matched' card is not a replicate of one already counted
				thisCardCounted = 0;
				while (cardsCounted[k] > -1) {
					if (cardsCounted[k] == j) {
						j = j + 1;//thisCardCounted = 1;
					}
					k = k + 1;
				}
				if (thisCardCounted == 0 && cardsCounted[k] == -1) {
					cardsCounted[k] = j;
					// card is still present
					sameCards = sameCards + 1;
					// card (or a replicate) is still in the same position
					if (i == j) {
						sameOrder += 1;
					}
					break;

				}
			}
		}
	}
	//print data
	printf("\t\t%d of %d of cards still in deck after shuffle\n", (int)(sameCards),oldDeckCount);
	if (1) {//sameOrder == oldDeckCount){
		printf("\t\t%d of %d of cards in the same position after shuffle\n", (int)sameOrder, state->deckCount[playerId]);
	}
	//assertions
	assert (sameCards == oldDeckCount);
	assert (sameOrder != oldDeckCount);
	return 0;
}

int main() {
    
    int seed = 1000;
    int numPlayer = 3;
	int i, j;
    
	struct gameState G;
	struct gameState *state = &G;
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, 
		   sea_hag, tribute, smithy};
	int deck[50] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, 
		   sea_hag, tribute, smithy, adventurer, gardens, embargo, village, minion, mine, cutpurse, 
		   sea_hag, tribute, smithy, adventurer, gardens, embargo, village, minion, mine, cutpurse, 
		   sea_hag, tribute, smithy, adventurer, gardens, embargo, village, minion, mine, cutpurse, 
		   sea_hag, tribute, smithy, adventurer, gardens, embargo, village, minion, mine, cutpurse, 
		   sea_hag, tribute, smithy};

	initializeGame(numPlayer, k, seed, &G);
	
	//test shuffle
		// new version of deck needs to have the exact same cards as before the shuffle
		// new version of deck must not be in the same order as before
	printf("Testing shuffle():\n");
	for (j=0;j<numPlayer;j++) {
		state->deckCount[j] = 50;
		for (i=0;i<50;i++) {
			state->deck[j][i] = deck[i];
		}
		testShuffle(&G, j);
	}
	printf("   Testing shuffle() passed!\n");	

    return 0;
}

